/*
 * Copyright 2013 Nikolay Todorov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jobinsight;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import com.jobinsight.util.ObjectType;

public class ViewerCellRenderer implements TableCellRenderer{
	private static FontMetrics tableCellFontMetrix = null;
	protected DefaultTableCellRenderer defaultRenderer;
	public static Color firstColumnBackground = null;
    public static Color evenRowBackground = Color.white;
   	public static Color oddRowBackground  = new Color(245, 245, 245);
   	JComponent rendererWithIcon = null;
   	private ImageIcon NON_PRIM_ICON = new ImageIcon(getClass().getResource("/img/etched_arrow.gif"));
   	
	public ViewerCellRenderer(){
		rendererWithIcon = createRendererWithIcon();
		defaultRenderer = new DefaultTableCellRenderer();
		
		if(tableCellFontMetrix==null){
			tableCellFontMetrix = defaultRenderer.getFontMetrics(defaultRenderer.getFont());
		}
	}
	
	protected Color getBackground(JTable table, int row, int column, boolean isSelected) {
		if(firstColumnBackground==null)
			firstColumnBackground = table.getTableHeader().getBackground();
		
		if(column==0)
			return firstColumnBackground;
		
		return isSelected ?
					null
					: row % 2 == 0 ? 
							evenRowBackground 
							: oddRowBackground;
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
		TableModel refTableModel = (TableModel)table.getModel();
		TableColumn refCol = table.getColumnModel().getColumn(column);

		
		JComponent renderer = (JComponent) defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		Color c = getBackground(table, row, column, isSelected);
		if(c!=null)
			renderer.setBackground(c);

		renderer = setValue(value, renderer, column);
		
		return renderer;
	}
	
	protected JComponent setValue(Object value, JComponent renderer, int column) {
		JLabel labelRenderer = null;
		if(renderer instanceof JLabel){
			labelRenderer = (JLabel) renderer;
			labelRenderer.setIcon(null);
		}

		if(value instanceof ViewerData){
			ViewerData data = (ViewerData)value;
			
			if( column==0 ){
				labelRenderer.setText(data.getKey());
				if(!ObjectType.isPrimitive(data.getObject())){
					renderer = setRendererWithIcon(labelRenderer);
				}
			}else{
				labelRenderer.setText(data.getValue());
			}
			
		}
		return renderer;
	}
	
	private JComponent setRendererWithIcon(JLabel labelRenderer){
		rendererWithIcon.add(labelRenderer, BorderLayout.WEST);
		return rendererWithIcon;
	}
	
	private JComponent createRendererWithIcon(){
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JLabel(NON_PRIM_ICON, JLabel.RIGHT), BorderLayout.EAST);
        return p;
	}

	/**
	 * 
	 */
	public static void setDefaultRender(JTable table) {
		ViewerCellRenderer renderer = new ViewerCellRenderer();
		table.setDefaultRenderer(ViewerData.class, renderer);
	}
}
